package HelloSeries.ui.prefs.elems.flextables.model;

import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

import HelloSeries.conf.Configuration;
import HelloSeries.metier.Tracker;
import HelloSeries.services.ServiceInvoker;

public class FlexTableTrackerModel extends AbstractTableModel {
	private String[] entetes;
	private ArrayList<Tracker> tracker_list;
	private String[] tab_priority;

	public FlexTableTrackerModel(String[] entetes) {
		this.entetes = entetes;
		this.tracker_list = Configuration.getInstance().getTrackerList().getList();
		tab_priority = new String[tracker_list.size()];
		for (int i = 0; i < tracker_list.size(); i++) {
			tab_priority[i]=(i+1)+"";
		}
	}

	public int getColumnCount() {
		return entetes.length;
	}

	public int getRowCount() {
		return tracker_list.size();
	}

	public Object getValueAt(int row, int col) {
		Tracker tracker = tracker_list.get(row);
		switch (col){
		case 0 : return tracker.isActive();
		case 1 : return tracker.getNomTracker().getValue();
		case 2 : return tracker.getPriority();
		default : return null;
		}
	}

	@Override
	public void fireTableCellUpdated(int row, int column) {
		super.fireTableCellUpdated(row, column);
		Tracker current = tracker_list.get(row);
		if (tracker_list.get(row).isPrivate() && tracker_list.get(row).isActive()){
			if (!ServiceInvoker.getInstance().searchCookie(current,true)){
				current.setActive(false);
			}
		}
	}
	
	public Class getColumnClass(int c) {
		return getValueAt(0, c).getClass();
	}

	public boolean isCellEditable(int row, int col) {
		return col != 1;
	}

	public ArrayList<Tracker> getTracker_list() {
		return tracker_list;
	}

	public String getColumnName(int column) {
		return entetes[column];
	}


	public void setValueAt(Object value, int row, int col) {
		Tracker tracker = tracker_list.get(row);
		switch (col){
		case 0 :
			tracker.setActive((Boolean)value);
			break;
		case 2 :  
			tracker.setPriority(value.toString().trim());
			break;
		}
		fireTableCellUpdated(row, col);
	}
	
	public void raisePopUp(String mess){
		JOptionPane.showMessageDialog(new JOptionPane(),mess,"Erreur",JOptionPane.ERROR_MESSAGE);
	}

	public String[] getTab_priority() {
		return tab_priority;
	}
}
